﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using Clipper.Model.EnumModel;
using Clipper.Model.DetectionModel;
using Clipper.Model.DataBaseModel;

namespace Clipper.Model.ViewModel
{
    /// <summary>
    /// 仪器监视类
    /// </summary>
    public class MonitorVM : AbstractModel
    {
        private ObservableCollection<Model.DataBaseModel.CupDetectResultInformation> detectResultList = new ObservableCollection<Model.DataBaseModel.CupDetectResultInformation>();//检测结果集合
        public ObservableCollection<Model.DataBaseModel.CupDetectResultInformation> DetectResultList
        {
            get
            {
                return detectResultList;
            }

            set
            {
                detectResultList = value;
                NotifyPropertyChanged("detectResultList");
            }
        }
        private ObservableCollection<CupDetectResultInformation> detectResultListToady = new ObservableCollection<CupDetectResultInformation>();//检测结果集合
        public ObservableCollection<CupDetectResultInformation> DetectResultListToady
        {
            get
            {
                return detectResultListToady;
            }

            set
            {
                detectResultListToady = value;
                NotifyPropertyChanged("detectResultListToady");
            }
        }

        private string detectTemp = "0"; //检测温度

        public string DetectTemp
        {
            get
            {
                return detectTemp;
            }

            set
            {
                detectTemp = value;
                NotifyPropertyChanged("DetectTemp");
            }
        }

        private int reagentTemp = 55; //试剂位温度
        public int ReagentTemp
        {
            get
            {
                return reagentTemp;
            }

            set
            {
                reagentTemp = value;
            }
        }


        private ObservableCollection<DisplayCup> displayCup;

        public ObservableCollection<DisplayCup> DisplayCup
        {
            get
            {
                return displayCup;
            }

            set
            {
                displayCup = value;
                NotifyPropertyChanged("DisplayCup");
            }
        }

        /// <summary>
        /// 检测结束标记属性  
        /// </summary>
        private DetectEndStatusEnum detectEndStatusEnum;
        public DetectEndStatusEnum DetectEndStatusEnum
        {
            get
            {
                return detectEndStatusEnum;
            }

            set
            {
                detectEndStatusEnum = value;
            }
        }

        //声明配置文件
        private MDC3500Configer mdcConfiger = new MDC3500Configer();
        public MDC3500Configer MdcConfiger
        {
            get
            {
                return mdcConfiger;
            }

            set
            {
                mdcConfiger = value;
            }
        }

        //结果标准表格
        ObservableCollection<Model.ResultReference> mResultReferenceList = new ObservableCollection<ResultReference>();
        public ObservableCollection<ResultReference> MResultReferenceList
        {
            get
            {
                return mResultReferenceList;
            }

            set
            {
                mResultReferenceList = value;
            }
        }


        //添加反应条的枚举类型状态
        ReactionStripStatusEnum reactionStripStatus;
        public ReactionStripStatusEnum ReactionStripStatus
        {
            get
            {
                return reactionStripStatus;
            }

            set
            {
                reactionStripStatus = value;
            }
        }

        //是否执行以下的函数   删除按钮点击时
        private WinDiolageDeleteEnum winDiolageDeleteEnum;
        public WinDiolageDeleteEnum WinDiolageDeleteEnum
        {
            get
            {
                return winDiolageDeleteEnum;
            }

            set
            {
                winDiolageDeleteEnum = value;
            }
        }
        //去判断选择性的进行展示图线
        private WinStandCurveSelectEnum winStandCurveSelectEnum;
        public WinStandCurveSelectEnum WinStandCurveSelectEnum
        {
            get
            {
                return winStandCurveSelectEnum;
            }

            set
            {
                winStandCurveSelectEnum = value;
            }
        }

        public Canvas MonitorCanvas
        {
            get;
            set;
        }

        /// <summary>
        /// 小车图像
        /// </summary>
        public FrameworkElement Transportation
        {
            get;
            set;
        }

        /// <summary>
        /// 是否正在运行检测流程,正常运行和暂停都为true
        /// </summary>
        public bool IsRunning
        {
            get;
            set;
        }

        bool _isBusy;
        /// <summary>
        /// 是否忙碌（显示等待界面）
        /// </summary>
        public bool IsBusy
        {
            get
            {
                return _isBusy;
            }
            set
            {
                _isBusy = value;
                NotifyPropertyChanged("IsBusy");
            }
        }


        #region 仪器检测进度

        #region 反应条

        ObservableCollection<ReactionStrip> _needStrips;
        /// <summary>
        /// 等待进行检测的反应条
        /// </summary>
        public ObservableCollection<ReactionStrip> NeedStrips
        {
            get
            {
                return _needStrips;
            }
            set
            {
                _needStrips = value;
                NotifyPropertyChanged("NeedStrips");
            }
        }

        #region 运行中的反应条
        ObservableCollection<ReactionStrip> _runningStrips;
        /// <summary>
        /// 运行中的反应条列表
        /// </summary>
        public ObservableCollection<ReactionStrip> RunningStrips
        {
            get
            {
                if (_runningStrips == null)
                {
                    _runningStrips = new ObservableCollection<ReactionStrip>();
                }
                return _runningStrips;
            }
            set
            {
                this.NotifyPropertyChanged("RunningStrips");
                _runningStrips = value;
            }
        }
        #endregion

        ObservableCollection<SampleInfomation> _sampleList = new ObservableCollection<SampleInfomation>();
        /// <summary>
        /// 运行中的反应试剂
        /// </summary>
        public ObservableCollection<SampleInfomation> SampletList
        {
            get
            {
                //if (_sampleList == null)
                //{
                //    _sampleList = new ObservableCollection<SampleInfomation>();
                //}
                return _sampleList;
            }
            set
            {
                //this.NotifyPropertyChanged("RunningCup");  //标记反应杯
                _sampleList = value;
            }
        }



        #region 运行中的反应试剂列表  
        ObservableCollection<ReagentInfomation> _reagentList = new ObservableCollection<ReagentInfomation>();
        /// <summary>
        /// 运行中的反应试剂
        /// </summary>
        public ObservableCollection<ReagentInfomation> ReagentList
        {
            get
            {
                return _reagentList;
            }
            set
            {
                //this.NotifyPropertyChanged("RunningCup");  //标记反应杯
                _reagentList = value;
            }
        }





        ObservableCollection<ReactionStrip> _incubationBoxReactionStrips;
        /// <summary>
        /// 孵育箱中反应条
        /// </summary>
        public ObservableCollection<ReactionStrip> IncubationBoxReactionStrips
        {
            get
            {
                if (_incubationBoxReactionStrips == null)
                {
                    _incubationBoxReactionStrips = new ObservableCollection<ReactionStrip>();
                }
                return _incubationBoxReactionStrips;
            }
            set
            {
                this.NotifyPropertyChanged("IncubationBoxReactionStrips");
                _incubationBoxReactionStrips = value;
            }
        }

        ObservableCollection<ReactionStrip> _waitReactionStrips;
        /// <summary>
        /// 恒温等待中的反应条
        /// </summary>
        public ObservableCollection<ReactionStrip> WaitReactionStrips
        {
            get
            {
                if (_waitReactionStrips == null)
                {
                    _waitReactionStrips = new ObservableCollection<ReactionStrip>();
                }
                return _waitReactionStrips;
            }
            set
            {
                this.NotifyPropertyChanged("WaitReactionStrips");
                _waitReactionStrips = value;
            }
        }

        ReactionStrip _R1Position = null;
        /// <summary>
        /// 试剂1位置的反应条
        /// </summary>
        public ReactionStrip R1Position
        {
            get
            {
                return _R1Position;
            }
            set
            {
                _R1Position = value;
                NotifyPropertyChanged("R1Position");
            }
        }
        ReactionStrip _R2Position = null;
        /// <summary>
        /// 试剂2位置反应条
        /// </summary>
        public ReactionStrip R2Position
        {
            get
            {
                return _R2Position;
            }
            set
            {
                _R2Position = value;
                NotifyPropertyChanged("R2Position");
            }
        }
        ReactionStrip _W1Position = null;
        /// <summary>
        /// 洗板1位置反应条
        /// </summary>
        public ReactionStrip W1Position
        {
            get
            {
                return _W1Position;
            }
            set
            {
                _W1Position = value;
                NotifyPropertyChanged("W1Position");
            }
        }
        ReactionStrip _W2Position = null;
        /// <summary>
        /// 洗板2位置反应条
        /// </summary>
        public ReactionStrip W2Position
        {
            get
            {
                return _W2Position;
            }
            set
            {
                _W2Position = value;
                NotifyPropertyChanged("W2Position");
            }
        }
        ReactionStrip _subPosition = null;
        /// <summary>
        /// 加底物位置反应条
        /// </summary>
        public ReactionStrip SubPosition
        {
            get
            {
                return _subPosition;
            }
            set
            {
                _subPosition = value;
                NotifyPropertyChanged("SubPosition");
            }
        }

        #endregion 

        #region 倒计时
        string _incubationNextCD = "00:00";
        /// <summary>
        /// 孵育仓出下一条倒计时
        /// </summary>
        public string IncubationNextCD
        {
            get
            {
                return _incubationNextCD;
            }
            set
            {
                _incubationNextCD = value;
                NotifyPropertyChanged("IncubationNextCD");
            }
        }

        string _nextResultCD = "00:00";
        /// <summary>
        /// 下一批检测结果时间
        /// </summary>
        public string NextResultCD
        {
            get
            {
                return _nextResultCD;
            }
            set
            {
                _nextResultCD = value;
                NotifyPropertyChanged("NextResultCD");
            }
        }

        string _deliveryCountDown = "00:00";
        /// <summary>
        /// 传送舱进样倒计时
        /// </summary>
        public string DeliveryCountDown
        {
            get
            {
                return _deliveryCountDown;
            }
            set
            {
                _deliveryCountDown = value;
                NotifyPropertyChanged("DeliveryCountDown");
            }
        }
        string _shelfInCountDown = "00:00";
        //样本架进样倒计时
        public string ShelfInCountDown
        {
            get
            {
                return _shelfInCountDown;
            }
            set
            {
                _shelfInCountDown = value;
                NotifyPropertyChanged("ShelfInCountDown");
            }
        }
        string _shelfOutCountDown = "00:00";
        /// <summary>
        /// 样本架出样倒计时
        /// </summary>
        public string ShelfOutCountDown
        {
            get
            {
                return _shelfOutCountDown;
            }
            set
            {
                _shelfOutCountDown = value;
                NotifyPropertyChanged("ShelfOutCountDown");
            }
        }
        string _completeCountDown = "00:00";
        /// <summary>
        /// 完成检测倒计时
        /// </summary>
        public string CompleteCountDown
        {
            get
            {
                return _completeCountDown;
            }
            set
            {
                _completeCountDown = value;
                NotifyPropertyChanged("CompleteCountDown");
            }
        }
        #endregion 

        ObservableCollection<ReactionStrip> _completeStrips;
        /// <summary>
        /// 完成的反应条
        /// </summary>
        public ObservableCollection<ReactionStrip> CompleteStrips
        {
            get
            {
                if (_completeStrips == null)
                {
                    _completeStrips = new ObservableCollection<ReactionStrip>();
                }
                return _completeStrips;
            }
            set
            {
                this.NotifyPropertyChanged("CompleteStrips");
                _completeStrips = value;
            }
        }
        #endregion


        #region 试管架
        ObservableCollection<TestTubeSelfInfo> _inTestTubeSelf;
        /// <summary>
        /// 试管架进样仓
        /// </summary>
        public ObservableCollection<TestTubeSelfInfo> InTestTubeSelf
        {
            get
            {
                if (_inTestTubeSelf == null)
                {
                    _inTestTubeSelf = new ObservableCollection<TestTubeSelfInfo>();
                }
                return _inTestTubeSelf;
            }
            set
            {
                _inTestTubeSelf = value;
                NotifyPropertyChanged("InTestTubeSelf");
            }
        }
        ObservableCollection<TestTubeSelfInfo> _outTestTubeSelf;
        /// <summary>
        /// 试管架回收仓
        /// </summary>
        public ObservableCollection<TestTubeSelfInfo> OutTestTubeSelf
        {
            get
            {
                if (_outTestTubeSelf == null)
                {
                    _outTestTubeSelf = new ObservableCollection<TestTubeSelfInfo>();
                }
                return _outTestTubeSelf;
            }
            set
            {
                _outTestTubeSelf = value;
                NotifyPropertyChanged("OutTestTubeSelf");
            }
        }
        int _inShelfCount;
        /// <summary>
        /// 进样仓中试管架的数量
        /// </summary>
        public int InShelfCount
        {
            get
            {
                return _inShelfCount;
            }
            set
            {
                _inShelfCount = value;
                NotifyPropertyChanged("InShelfCount");
            }
        }
        int _outShelfCount;
        /// <summary>
        /// 回收仓中试管架的数量
        /// </summary>
        public int OutShelfCount
        {
            get
            {
                return _outShelfCount;
            }
            set
            {
                _outShelfCount = value;
                NotifyPropertyChanged("OutShelfCount");
            }
        }
        TestTubeSelfInfo _runningTestTubeShelf = null;
        /// <summary>
        /// 在取样位置的试管架
        /// </summary>
        public TestTubeSelfInfo RunningTestTubeShelf
        {
            get
            {
                return _runningTestTubeShelf;
            }
            set
            {
                _runningTestTubeShelf = value;
                NotifyPropertyChanged("RunningTestTubeShelf");
            }
        }
        #endregion 试管架


        #endregion

        #region 仪器状态属性

        ComponentStateEnum _isCheckSelfSuccess;
        /// <summary>
        /// 仪器是否自检通过
        /// </summary>
        public ComponentStateEnum IsCheckSelfSuccess
        {
            get
            {
                return _isCheckSelfSuccess;
            }
            set
            {
                _isCheckSelfSuccess = value;
                NotifyPropertyChanged("IsCheckSelfSuccess");
            }
        }

        ComponentStateEnum _reagentPlate1State;
        /// <summary>
        /// 试剂盘1状态
        /// </summary>
        public ComponentStateEnum ReagentPlate1State
        {
            get
            {
                return _reagentPlate1State;
            }
            set
            {
                _reagentPlate1State = value;
                this.NotifyPropertyChanged("ReagentPlate1State");
            }
        }
        ComponentStateEnum _reagentPlate2State;
        /// <summary>
        /// 试剂盘2状态
        /// </summary>
        public ComponentStateEnum ReagentPlate2State
        {
            get
            {

                return _reagentPlate2State;
            }
            set
            {
                _reagentPlate2State = value;
                this.NotifyPropertyChanged("ReagentPlate2State");
            }
        }
        double _incubatorTempurature = 0;
        /// <summary>
        /// 孵育区温度
        /// </summary>
        public double IncubatorTempurature
        {
            get
            {
                return _incubatorTempurature;
            }
            set
            {
                _incubatorTempurature = value;
                this.NotifyPropertyChanged("IncubatorTempurature");
            }
        }
        double _KitTempurature = 0;
        /// <summary>
        /// 试剂区温度
        /// </summary>
        public double KitTempurature
        {
            get
            {
                return _KitTempurature;
            }
            set
            {
                _KitTempurature = value;
                NotifyPropertyChanged("KitTempurature");
            }
        }
        ComponentStateEnum _incubationBoxState;
        /// <summary>
        /// 反应条传送仓状态
        /// </summary>
        public ComponentStateEnum IncubationBoxState
        {
            get
            {
                return _incubationBoxState;
            }
            set
            {
                _incubationBoxState = value;
                NotifyPropertyChanged("IncubationBoxState");
            }
        }

        int _reactionStripHouseCount;
        /// <summary>
        /// 反应条传送仓中反应条数量
        /// </summary>
        public int ReactionStripHouseCount
        {
            get
            {
                return _reactionStripHouseCount;
            }
            set
            {
                _reactionStripHouseCount = value;
                this.NotifyPropertyChanged("ReactionStripHouseCount");
            }
        }
        ComponentStateEnum _reactionStripHouseState;
        /// <summary>
        /// 反应条传送仓状态
        /// </summary>
        public ComponentStateEnum ReactionStripHouseState
        {
            get
            {
                return _reactionStripHouseState;
            }
            set
            {
                _reactionStripHouseState = value;
                NotifyPropertyChanged("ReactionStripHouseState");
            }
        }
        ComponentStateEnum _sampleHouseInState;
        /// <summary>
        /// 样本进样仓状态
        /// </summary>
        public ComponentStateEnum SampleHouseInState
        {
            get
            {
                return _sampleHouseInState;
            }
            set
            {
                _sampleHouseInState = value;
                NotifyPropertyChanged("SampleHouseInState");
            }
        }
        ComponentStateEnum _sampleHouseOutState;
        /// <summary>
        /// 样本回收仓状态
        /// </summary>
        public ComponentStateEnum SampleHouseOutState
        {
            get
            {
                return _sampleHouseOutState;
            }
            set
            {
                _sampleHouseOutState = value;
                NotifyPropertyChanged("SampleHouseOutState");
            }
        }
        float _waitDetectHouseTempurature;
        /// <summary>
        /// 恒温等待的反应条温度
        /// </summary>
        public float WaitDetectHouseTempurature
        {
            get
            {
                return _waitDetectHouseTempurature;
            }
            set
            {
                _waitDetectHouseTempurature = value;
            }
        }
        ComponentStateEnum _rinseFlaskState;
        /// <summary>
        /// 冲洗液瓶状态
        /// </summary>
        public ComponentStateEnum RinseFlaskState
        {
            get
            {
                return _rinseFlaskState;
            }
            set
            {
                _rinseFlaskState = value;
                NotifyPropertyChanged("RinseFlaskState");
            }
        }
        ComponentStateEnum _substrateState;
        /// <summary>
        /// 底物瓶状态
        /// </summary>
        public ComponentStateEnum SubstrateState
        {
            get
            {
                return _substrateState;
            }
            set
            {
                _substrateState = value;
                NotifyPropertyChanged("SubstrateState");
            }
        }
        ComponentStateEnum _recycleState;
        /// <summary>
        /// 回收仓状态
        /// </summary>
        public ComponentStateEnum RecycleState
        {
            get
            {
                return _recycleState;
            }
            set
            {
                _recycleState = value;
                NotifyPropertyChanged("RecycleState");
            }
        }
        ComponentStateEnum _washFlaskState;
        /// <summary>
        /// 洗涤液状态
        /// </summary>
        public ComponentStateEnum WashFlaskState
        {
            get
            {
                return _washFlaskState;
            }
            set
            {
                _washFlaskState = value;
                NotifyPropertyChanged("WashFlaskState");
            }
        }
        ComponentStateEnum _discardSolutionState;
        /// <summary>
        /// 废液状态
        /// </summary>
        public ComponentStateEnum DiscardSolutionState
        {
            get
            {
                return _discardSolutionState;
            }
            set
            {
                _discardSolutionState = value;
                NotifyPropertyChanged("DiscardSolutionState");
            }
        }
        ComponentStateEnum _clipperState;
        /// <summary>
        /// 仪器状态
        /// </summary>
        public ComponentStateEnum ClipperState
        {
            get
            {
                return _clipperState;
            }
            set
            {
                _clipperState = value;
                NotifyPropertyChanged("ClipperState");
            }
        }
        int _maintainDays;
        /// <summary>
        /// 上次维修后的天数
        /// </summary>
        public int MaintainDays
        {
            get
            {
                return _maintainDays;
            }
            set
            {
                _maintainDays = value;
                NotifyPropertyChanged("MaintainDays");
            }
        }
        ComponentStateEnum _USBConnectState;
        /// <summary>
        /// 仪器通信状态
        /// </summary>
        public ComponentStateEnum USBConnectState
        {
            get
            {
                return _USBConnectState;
            }
            set
            {
                _USBConnectState = value;
                NotifyPropertyChanged("USBConnectState");
            }
        }
        ComponentStateEnum _LISConnectState;
        /// <summary>
        /// LIS连接状态
        /// </summary>
        public ComponentStateEnum LISConnectState
        {
            get
            {
                return _LISConnectState;
            }
            set
            {
                _LISConnectState = value;
                NotifyPropertyChanged("LISConnectState");
            }
        }  





        #endregion


        public List<double> GetConcentrationList(StandardCurveData standardCurve)
        {
            List<double> concentrationList = new List<double>();  //浓度集合            
            if (standardCurve.Concentration1 != 0)
            {
                concentrationList.Add(standardCurve.Concentration1);
            }
            if (standardCurve.Concentration2 != 0)
            {
                concentrationList.Add(standardCurve.Concentration2);
            }
            if (standardCurve.Concentration3 != 0)
            {
                concentrationList.Add(standardCurve.Concentration3);
            }
            if (standardCurve.Concentration4 != 0)
            {
                concentrationList.Add(standardCurve.Concentration4);
            }
            if (standardCurve.Concentration5 != 0)
            {
                concentrationList.Add(standardCurve.Concentration5);
            }
            if (standardCurve.Concentration6 != 0)
            {
                concentrationList.Add(standardCurve.Concentration6);
            }
            return concentrationList;
        }

        public List<double> GetDetectResultList(StandardCurveData standardCurve)
        {
            List<double> detectResultList = new List<double>(); //检测结果集合
            if (standardCurve.DetectResult1 != 0)
            {
                detectResultList.Add(standardCurve.DetectResult1);
            }
            if (standardCurve.DetectResult2 != 0)
            {
                detectResultList.Add(standardCurve.DetectResult2);
            }
            if (standardCurve.DetectResult3 != 0)
            {
                detectResultList.Add(standardCurve.DetectResult3);
            }
            if (standardCurve.DetectResult4 != 0)
            {
                detectResultList.Add(standardCurve.DetectResult4);
            }
            if (standardCurve.DetectResult5 != 0)
            {
                detectResultList.Add(standardCurve.DetectResult5);
            }
            if (standardCurve.DetectResult6 != 0)
            {
                detectResultList.Add(standardCurve.DetectResult6);
            }
            return detectResultList;
        }

        /// <summary>
        /// m_x是检测浓度 ，m_y是检测结果
        /// </summary>
        /// <param name="m_x"></param>
        /// <param name="m_y"></param>
        /// <returns></returns>
        /// 
        double xAverage = 0; // x的平均数
        double yAverage = 0;  // y的平均数
        public double getLineValue(List<double> m_x, List<double> m_y) //  m_x是检测浓度 ，m_y是检测结果
        {

            double X = 0;
            foreach (double concentration in m_x)
            {
                X += concentration;
            }

            double y = 0;
            foreach (double detectResult in m_y)
            {
                y += detectResult;  //检测结果集合
            }
            xAverage = X / m_y.Count;  // x的平均数
            yAverage = y / m_y.Count;  // y的平均数

            double countResult = 0; //表达式上面的结果
            double reagentGan = 0;  //表达式下面的结果
            double yreagentGan = 0;
            for (int i = 0; i < m_y.Count; i++)
            {
                countResult += (m_x[i] - xAverage) * (m_y[i] - yAverage);
                reagentGan += Math.Pow((m_x[i] - xAverage), 2);
                yreagentGan += Math.Pow((m_y[i] - yAverage), 2);
            }
            return Math.Pow((countResult / Math.Sqrt(reagentGan * yreagentGan)), 2);

            //int r =∑(Xi - X的平均数)(Yi - Y平均数) / 根号下[∑(Xi - X平均数) ^ 2 *∑(Yi - Y平均数) ^ 2]
        }


        /// <summary>
        /// 计算系数b的值
        /// </summary>
        /// <param name="m_x"></param>
        /// <param name="m_y"></param>
        /// <returns></returns>
        public List<double> setCoefficientB(List<double> m_x, List<double> m_y) //m_x是检测浓度集合 ，m_y是检测结果集合
        {
            List<double> resultList = new List<double>();

            double X = 0;
            foreach (double concentration in m_x)
            {
                X += concentration;
            }

            double y = 0;
            foreach (double detectResult in m_y)
            {
                y += detectResult;  //检测结果集合
            }
            double xAverage = X / m_y.Count;  // x的平均数
            double yAverage = y / m_y.Count;  // y的平均数
            resultList.Add(xAverage);
            resultList.Add(yAverage);

            double topCount = 0;
            double bottomCount = 0;
            for (int j = 0; j < m_x.Count; j++)
            {
                topCount += (m_x[j] - xAverage) * (m_y[j] - yAverage);
                bottomCount += Math.Pow((m_x[j] - xAverage), 2);
            }
            resultList.Add(Math.Round((topCount / bottomCount), 3));
            return resultList;
        }

    }
}
